package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

import model.Board;
import model.User;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import service.BoardService;
import util.SaveFile;
import action.base.BaseAction;

@Scope("prototype")
@Controller
public class BoardAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Autowired
	private BoardService boardService;
	private PageList<HashMap<String, Object>> pageList;
	private boolean success = false;
	private Board board;
	private int total;
	private File file;
	// 获取上传文件的名称
	private String fileFileName;
	private InputStream stream;
	private Integer boardId;

	public String saveOrUpdate() {
		User user = super.getUser();
		board.setPublisher(user.getId());
		board.setPublishername(user.getName());
		boardService.saveOrUpdate(board);
		if (file != null) {
			String filePath = saveFile();
			board.setAttach(filePath);
			board.setAttachname(fileFileName);
			boardService.saveOrUpdate(board);
		}
		success = true;
		return "success";
	}

	public String delete() {
		String ids = super.getParameterStr("ids");
		boardService.delete(ids);
		success = true;
		return "success";
	}

	public String publish() {
		String ids = super.getParameterStr("ids");
		boardService.publish(ids);
		success = true;
		return "success";
	}

	public String queryPage() throws UnsupportedEncodingException {
		int page = super.getParameterInt("page");
		int limit = super.getParameterInt("limit");
		User user = super.getUser();
		String title = super.getParameterStr("title");
		String beginDate = super.getParameterStr("beginDate");
		String endDate = super.getParameterStr("endDate");
		Integer allFlag = super.getParameterInt("allFlag");// 1.个人工作台请求
		HashMap<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isNotBlank(title)) {
			title = null;
		} else {
			byte[] _title = title.getBytes("ISO-8859-1");
			title = new String(_title, "UTF-8");
		}
		if (!StringUtils.isNotBlank(beginDate)) {
			beginDate = null;
		}
		if (!StringUtils.isNotBlank(endDate)) {
			endDate = null;
		}
		if (allFlag == null) {
			allFlag = 0;
		}
		map.put("title", title);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		map.put("temp", user.getId());
		pageList = boardService.queryPage(page, limit, allFlag, map);
		setTotal(pageList.getPaginator().getTotalCount());
		return "queryPage";
	}

	public String getById() {
		Integer boardid = super.getParameterInt("boardid");
		board = boardService.getById(boardid);
		setSuccess(true);
		return "boardInfo";
	}

	public String saveFile() {
		Integer _fname;
		if (boardId == null) {
			_fname = board.getId();
		} else {
			_fname = boardId;
		}

		SaveFile sf = new SaveFile();
		String path = super.getContext().getRealPath(
				"\\uploadFiles\\boardFiles\\f_");

		String filePath = null;
		String fname = _fname.toString();
		if (file != null && file.exists() && file.length() > 0) {
			filePath = sf.save2Server(fname, file, path);
		}
		return filePath;
	}

	public String down() throws Exception {
		Integer id = super.getParameterInt("boardid");
		String fname = super.getParameterStr("fileName");
		board = boardService.getById(id);
		if (StringUtils.isNotEmpty(board.getAttach())) {
			String attach = board.getAttach();
			File f = new File(attach);
			if (f.exists()) {
				setStream(new FileInputStream(f));

			} else
				return "noFile";
		} else
			return "noFile";
		success = true;
		fileFileName = fname;
		return "down";

	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public InputStream getStream() {
		return stream;
	}

	public void setStream(InputStream stream) {
		this.stream = stream;
	}

	public Integer getBoardId() {
		return boardId;
	}

	public void setBoardId(Integer boardId) {
		this.boardId = boardId;
	}

	public Board getBoard() {
		return board;
	}

	public void setBoard(Board board) {
		this.board = board;
	}

	public PageList<HashMap<String, Object>> getPageList() {
		return pageList;
	}

	public void setPageList(PageList<HashMap<String, Object>> pageList) {
		this.pageList = pageList;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

}
